import { getLocalHistories, setLocalHistories } from '@/utils/storage';
import { union } from 'lodash';
import { useState } from 'react';

export default function useHistories() {
  const [histories, setHistories] = useState(getLocalHistories());

  const handleAddHistories = (key: string) => {
    const localHistories = getLocalHistories();

    const newList = union([...localHistories], [key]);
    setLocalHistories(newList);
    setHistories(newList);
  };

  const handleClearHistories = () => {
    setLocalHistories([]);
    setHistories([]);
  };

  return { histories, handleAddHistories, handleClearHistories };
}
